"""测试运行脚本"""
import subprocess
import sys
import os


def run_pytest_tests():
    """运行Pytest测试并生成Allure报告"""
    print("🚀 开始运行Pytest测试套件...")
    
    # 运行Pytest测试
    result = subprocess.run([
        'python3', '-m', 'pytest',
        'tests/test_baidu_pytest.py',
        '-v',
        '--alluredir=allure-results',
        '--clean-alluredir'
    ], capture_output=True, text=True)
    
    # 打印测试输出
    print("=== Pytest输出 ===")
    print(result.stdout)
    if result.stderr:
        print("=== 错误信息 ===")
        print(result.stderr)
    
    # 生成Allure报告
    if result.returncode == 0 or os.path.exists('allure-results'):
        print("\n📊 生成Allure测试报告...")
        subprocess.run(['allure', 'generate', 'allure-results', '-o', 'allure-report', '--clean'])
        
        print("🎉 测试完成！")
        print("📁 测试报告位置: file://" + os.path.abspath("allure-report/index.html"))
    else:
        print("❌ 测试运行失败")
    
    return result.returncode


if __name__ == "__main__":
    exit_code = run_pytest_tests()
    sys.exit(exit_code)

